Public Class frmTheoDoiNhapHangHoa
    Dim dataPhieuNhap As DataTable
    Dim dataChiTietPhieuNhap As DataTable

    Dim sqlQuery As String = ""

    '***********************************
    Private Sub loadPhieuNhap(ByVal tuNgay As Date, ByVal denNgay As Date)
        sqlQuery = "SELECT nhh.ma_chungtu, nhh.ngay_chungtu, nhh.so_hoadon, nhh.ngay_hoadon, ncc.ten_nhacungcap, k.ten_kho, nhh.diengiai "
        sqlQuery += "FROM tbl_nhap_hanghoa as nhh, tbl_nhacungcap as ncc, tbl_kho as k "
        sqlQuery += "WHERE nhh.ma_nhacungcap = ncc.ma_nhacungcap "
        sqlQuery += "AND nhh.ma_kho = k.ma_kho "
        sqlQuery += "AND nhh.ngay_chungtu BETWEEN '" & Format(tuNgay, "yyyy-MM-dd") & "' AND '" & Format(denNgay, "yyyy-MM-dd") & "' "
        sqlQuery += "ORDER BY nhh.ngay_chungtu DESC"
        dataPhieuNhap = moduleDatabaseConn.GetDataTable(sqlQuery)
        Me.gridPhieuNhap.DataSource = dataPhieuNhap
        Me.lblTotal.Text = dataPhieuNhap.Rows.Count
    End Sub

    Private Sub loadChiTietPhieuNhap(ByVal ma_chungtu As String)
        sqlQuery = "SELECT ncthh.ma_hanghoa, hh.ten_bietduoc, hh.donvi_banchan, ncthh.solo, ncthh.handung, ncthh.soluong, ncthh.ngay_sanxuat, k.ten_ke "
        sqlQuery += "FROM tbl_nhap_chitiet_hanghoa as ncthh, tbl_hanghoa as hh, tbl_ke as k "
        sqlQuery += "WHERE ncthh.ma_hanghoa = hh.ma_hanghoa "
        sqlQuery += "AND ncthh.ma_ke = k.ma_ke "
        sqlQuery += "AND ncthh.ma_chungtu = '" & ma_chungtu & "' "
        sqlQuery += "GROUP BY ncthh.ma_hanghoa, ncthh.solo "
        sqlQuery += "ORDER BY hh.ten_bietduoc"
        dataChiTietPhieuNhap = moduleDatabaseConn.GetDataTable(sqlQuery)
        Me.gridChiTietPhieuNhap.DataSource = dataChiTietPhieuNhap
        Me.lblTotalChiTiet.Text = dataChiTietPhieuNhap.Rows.Count
    End Sub
    '***********************************

    Private Sub frmTheoDoiNhapHangHoa_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        moduleUtils.setDinhDangVietNam()
        Me.txtTuNgay.Text = moduleUtils.getNgayDauThang(Today.Year, Today.Month)
        Me.txtDenNgay.Text = moduleUtils.getNgayCuoiThang(Today.Year, Today.Month)
        Me.btnDanhSach.Focus()
    End Sub

    Private Sub btnDanhSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDanhSach.Click
        loadPhieuNhap(CDate(Me.txtTuNgay.Text.Trim), CDate(Me.txtDenNgay.Text.Trim))
    End Sub

    Private Sub gridPhieuNhap_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridPhieuNhap.CellClick
        If e.RowIndex > -1 Then
            loadChiTietPhieuNhap(Me.gridPhieuNhap.Item(0, e.RowIndex).Value)
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub DPTDenNgay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DPTDenNgay.ValueChanged
        Me.txtDenNgay.Text = Me.DPTDenNgay.Value
    End Sub
    Private Sub DPTTuNgay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DPTTuNgay.ValueChanged
        Me.txtTuNgay.Text = Me.DPTTuNgay.Value
    End Sub
End Class